//
//  Base64Encry.m
//  werfgh
//
//  Created by 李晓勇 on 15-3-1.
//  Copyright (c) 2015年 dingshenwangluokeji. All rights reserved.
//

#import "Base64Encry.h"
#import "SecurityUtil.h"
@implementation Base64Encry
//post请求  Base64加密参数  base64中“＝”转换“PQ”
+ (NSString*)switchBase64String:(NSString *)string
{
    NSString *str = [SecurityUtil encodeBase64String:string];
    //对编码后的参数进行替换
    str=[str stringByReplacingOccurrencesOfString:@"=" withString:@"PQ"];

    return str;
}
+ (NSDictionary*)switchUrlString:(NSString *)urlString switchParameterString:(NSString*)parameterString
{
//    urlString=@"http://oemapi2.leehuu.cn/YXBp/dm9zb2Vt/TG9nb24PQ";
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    //第二步，创建请求
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    
    [request setHTTPMethod:@"POST"];
    //设置请求方式为POST，默认为GET
    
    //Base64编码参数
//    parameterString=[NSString stringWithFormat:@"dXNlck5hbWUPQ=%@&password=321&Os=2&proxyId=62634&vCode=1",[self switchBase64String:@"18710011594"]];
//    NSLog(@"ttt tt %@",parameterString);
    //设置参数
    
    NSData *data = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
    //
    NSLog(@"00%@",data);
    [request setHTTPBody:data];
    __autoreleasing NSError *error;
    __autoreleasing NSURLResponse *response;
    //第三步，连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"11%@",received);
    NSDictionary*dic;
    if (error)
    {
        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"网络环境不好，请稍后再试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        
    }
    else
    {
        
        dic= [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:nil];
    }
    return dic;
}
@end
